package com.currency.mall.ui.login;

import android.content.Context;

import com.currency.mall.Constant;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.util.MD5Util;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import java.util.HashMap;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/22.
 */

public class RegisterPresent implements RegisterContract.Presenter {

    private RegisterContract.View mView;

    public RegisterPresent(RegisterContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void sendCode(String type) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("phoneOremail", mView.getPhone());
        hashMap.put("tempName", "9374734");
        hashMap.put("imgCode", mView.getPhoneVerifyCode());
        hashMap.put("type", type);
        mView.showLoading();
        NetWork.getLoginApi().sendcode(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideLoading();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.hideLoading();
                        String code = responseBean.getCode();
                        if (code.equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("短信发送成功，请注意查收,5分钟内有效", 200);
                            mView.sendCodesuccess();
                        }else if(code.equals("1011")){
                            ToastUitl.show(responseBean.getMsg(), 200);
                            mView.phoneVerifyCodeFail();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }

                    }
                });
    }

    @Override
    public void checkCode() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", mView.getVerifyCode());
        hashMap.put("mobile", mView.getPhone());
        NetWork.getLoginApi().checkcode(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        String code = responseBean.getCode();
                        if (code.equals(Constant.SUCCESS_CODE)) {
                            mView.checkCodesuccess();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }

                    }
                });
    }

    @Override
    public void register() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("passwd", MD5Util.toMD5(mView.getpwd()).toLowerCase());
        hashMap.put("mobile", mView.getPhone());
        hashMap.put("agencyCode", mView.getProxyCode());
        mView.showLoading();
        NetWork.getLoginApi().register(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideLoading();
                        mView.showNetError();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.hideLoading();
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("注册成功,请登录", 200);
                            mView.toLogin();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }

                    }
                });
    }

    @Override
    public void forgetPwd(Context context) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("passwd", MD5Util.toMD5(mView.getpwd()).toLowerCase());
        hashMap.put("mobile", PreferenceUtils.getUserMobile(context));
        mView.showLoading();
        NetWork.getLoginApi().setPwd(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideLoading();
                        mView.showNetError();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.hideLoading();
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("密码设置成功,请登录", 200);
                            mView.toLogin();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }

                    }
                });
    }
}
